## Demogrpahic Summary Statistics of Survey Respondents
##
## Kevin Quinn
## 10/5/2021
##

mydata <- read.csv("../ScaleRaceSpring2017clean.csv")


mydata <- mydata[, c(442:444, 468)]
mydata$Age <- 2017 - mydata$R.age_1

mydata$Age.15.19 <- mydata$Age >= 15 & mydata$Age <= 19
mydata$Age.20.24 <- mydata$Age >= 20 & mydata$Age <= 24
mydata$Age.25.34 <- mydata$Age >= 25 & mydata$Age <= 34
mydata$Age.35.44 <- mydata$Age >= 35 & mydata$Age <= 44
mydata$Age.45.54 <- mydata$Age >= 45 & mydata$Age <= 54
mydata$Age.55.64 <- mydata$Age >= 55 & mydata$Age <= 64
mydata$Age.65.74 <- mydata$Age >= 65 & mydata$Age <= 74
mydata$Age.75.84 <- mydata$Age >= 75 & mydata$Age <= 84
mydata$Age.85plus <- mydata$Age >= 85



mydata <- mydata[!is.na(mydata$Race), ]
mydata.black <- mydata[mydata$Race == "Black", ]
mydata.white <- mydata[mydata$Race == "White", ]



## age among black respondents
category <- NULL
age.cat.n <- NULL
age.cat.pct <- NULL
for (v in 6:14){
    category <- c(category, colnames(mydata.black)[v])
    age.cat.n <- c(age.cat.n, sum(mydata.black[,v], na.rm=TRUE))
    age.cat.pct <- round(c(age.cat.pct,
                     100 * (sum(mydata.black[,v], na.rm=TRUE) /
                            nrow(mydata.black))), 1) 
}
## missing
category <- c(category, "Missing")
age.cat.n <- c(age.cat.n, sum(is.na(mydata.black[,v])))
age.cat.pct <- round(c(age.cat.pct,
                       100 * (sum(is.na(mydata.black[,v])) /
                              nrow(mydata.black))), 1) 

black.age.table <- data.frame(Age.Category=category,
                              N=age.cat.n, Pct=age.cat.pct)





## age among white respondents
category <- NULL
age.cat.n <- NULL
age.cat.pct <- NULL
for (v in 6:14){
    category <- c(category, colnames(mydata.white)[v])
    age.cat.n <- c(age.cat.n, sum(mydata.white[,v], na.rm=TRUE))
    age.cat.pct <- round(c(age.cat.pct,
                     100 * (sum(mydata.white[,v], na.rm=TRUE) /
                            nrow(mydata.white))), 1) 
}
## missing
category <- c(category, "Missing")
age.cat.n <- c(age.cat.n, sum(is.na(mydata.white[,v])))
age.cat.pct <- round(c(age.cat.pct,
                       100 * (sum(is.na(mydata.white[,v])) /
                              nrow(mydata.white))), 1) 

white.age.table <- data.frame(Age.Category=category,
                              N=age.cat.n, Pct=age.cat.pct)










## gender among black respondents
category <- NULL
gender.cat.n <- NULL
gender.cat.pct <- NULL
category <- c(category, "Female")
gender.cat.n <- c(gender.cat.n,
                  sum(mydata.black$R.gender == "Female", na.rm=TRUE))
gender.cat.pct <- c(gender.cat.pct,
                    round(100 * (sum(mydata.black$R.gender == "Female",
                                     na.rm=TRUE) /
                                 nrow(mydata.black)), 1))

category <- c(category, "Male")
gender.cat.n <- c(gender.cat.n,
                  sum(mydata.black$R.gender == "Male", na.rm=TRUE))
gender.cat.pct <- c(gender.cat.pct,
                    round(100 * (sum(mydata.black$R.gender == "Male",
                                     na.rm=TRUE) /
                                 nrow(mydata.black)), 1))

## missing data
category <- c(category, "Missing")
gender.cat.n <- c(gender.cat.n,
                  sum(is.na(mydata.black$R.gender)))
gender.cat.pct <- c(gender.cat.pct,
                    round(100 * (sum(is.na(mydata.black$R.gender)) /
                                 nrow(mydata.black)), 1))


black.gender.table <- data.frame(Gender=category,
                                 N=gender.cat.n,
                                 Pct=gender.cat.pct)








## gender among white respondents
category <- NULL
gender.cat.n <- NULL
gender.cat.pct <- NULL
category <- c(category, "Female")
gender.cat.n <- c(gender.cat.n,
                  sum(mydata.white$R.gender == "Female", na.rm=TRUE))
gender.cat.pct <- c(gender.cat.pct,
                    round(100 * (sum(mydata.white$R.gender == "Female",
                                     na.rm=TRUE) /
                                 nrow(mydata.white)), 1))

category <- c(category, "Male")
gender.cat.n <- c(gender.cat.n,
                  sum(mydata.white$R.gender == "Male", na.rm=TRUE))
gender.cat.pct <- c(gender.cat.pct,
                    round(100 * (sum(mydata.white$R.gender == "Male",
                                     na.rm=TRUE) /
                                 nrow(mydata.white)), 1))

## missing data
category <- c(category, "Missing")
gender.cat.n <- c(gender.cat.n,
                  sum(is.na(mydata.white$R.gender)))
gender.cat.pct <- c(gender.cat.pct,
                    round(100 * (sum(is.na(mydata.white$R.gender)) /
                                 nrow(mydata.white)), 1))


white.gender.table <- data.frame(Gender=category,
                                 N=gender.cat.n,
                                 Pct=gender.cat.pct)







## education among black respondents
educ.levels <- c("Did not finish high school",
                 "High school graduate",
                 "Some college",
                 "College graduate (bachelor's degree)",
                 "Graduate school")
educ.cat.n <- NULL
educ.cat.pct <- NULL
for (educ in educ.levels){
    educ.cat.n <- c(educ.cat.n, sum(mydata.black$R.edu == educ))
    educ.cat.pct <- c(educ.cat.pct,
                      round(100 * (sum(mydata.black$R.edu == educ,
                                       na.rm=TRUE) /
                                   nrow(mydata.black)), 1))    
}
## missing data
educ.levels <- c(educ.levels, "Missing")
educ.cat.n <- c(educ.cat.n,
                  sum(is.na(mydata.black$R.edu)))
educ.cat.pct <- c(educ.cat.pct,
                    round(100 * (sum(is.na(mydata.black$R.edu)) /
                                 nrow(mydata.black)), 1))


black.educ.table <- data.frame(Education=educ.levels,
                               N=educ.cat.n,
                               Pct=educ.cat.pct)





    



## education among white respondents
educ.levels <- c("Did not finish high school",
                 "High school graduate",
                 "Some college",
                 "College graduate (bachelor's degree)",
                 "Graduate school")
educ.cat.n <- NULL
educ.cat.pct <- NULL
for (educ in educ.levels){
    educ.cat.n <- c(educ.cat.n, sum(mydata.white$R.edu == educ))
    educ.cat.pct <- c(educ.cat.pct,
                      round(100 * (sum(mydata.white$R.edu == educ,
                                       na.rm=TRUE) /
                                   nrow(mydata.white)), 1))    
}
## missing data
educ.levels <- c(educ.levels, "Missing")
educ.cat.n <- c(educ.cat.n,
                  sum(is.na(mydata.white$R.edu)))
educ.cat.pct <- c(educ.cat.pct,
                    round(100 * (sum(is.na(mydata.white$R.edu)) /
                                 nrow(mydata.white)), 1))


white.educ.table <- data.frame(Education=educ.levels,
                               N=educ.cat.n,
                               Pct=educ.cat.pct)





library(xtable)

    
sink("DemographicTables-Raw.txt")
print(xtable(black.age.table, caption="Age Among Black Respondents", digits=1),
      include.rownames=FALSE)
print(xtable(white.age.table, caption="Age Among White Respondents", digits=1),
      include.rownames=FALSE)


print(xtable(black.gender.table, caption="Gender Among Black Respondents",
             digits=1),
      include.rownames=FALSE)
print(xtable(white.gender.table, caption="Gender Among White Respondents",
             digits=1),
      include.rownames=FALSE)

print(xtable(black.educ.table, caption="Education Among Black Respondents",
             digits=1),
      include.rownames=FALSE)
print(xtable(white.educ.table, caption="Education Among White Respondents",
             digits=1),
      include.rownames=FALSE)




sink()

